package com.example.javaproject;

import java.util.Calendar;
import java.util.TimeZone;

/**
 * 使用前一定要调用{@link TimeChecker#reset()}方法 和{@link TimeChecker#setTimezone(String)}
 * 在设置了时间和日期后，会对输入和当前的时间&日期进行比较
 * Created by zjx8969 on 2018/2/1.
 */

public class TimeChecker {
    private Calendar calendar;
    private String year;
    private String moth;
    private String day;
    private String hour;
    private String minute;

    public void reset() {
        year = moth = day = hour = minute = null;
    }

    public void setTimezone(String timezone) {
        if (timezone == null || timezone.isEmpty()) {
            calendar = Calendar.getInstance();
        } else {
            TimeZone timeZone = TimeZone.getTimeZone(timezone);
            calendar = Calendar.getInstance(timeZone);
        }
    }

    /**
     * @param dateString 格式如"01-29-2018"
     */
    public void setDate(String dateString) {
        if (dateString == null || dateString.trim().length() == 0) return;
        String[] split = dateString.split("-");
        if (split.length != 3) return;
        year = split[2];
        moth = split[0];
        day = split[1];
    }

    /**
     * @param timeString 格式 10:34
     */
    public void setTime(String timeString) {
        if (timeString == null || timeString.trim().length() == 0) return;
        String[] times = timeString.split(":");
        if (times.length != 2) return;
        hour = times[0];
        minute = times[1];
    }

    public void setTime(String hour, String minute) {
        this.hour = hour;
        this.minute = minute;
    }

    public void setDate(String year, String moth, String day) {
        this.year = year;
        this.moth = moth;
        this.day = day;
    }


    public boolean checkDateTimeValidate() {
        int yearInt = calendar.get(Calendar.YEAR);
        int mothInt = calendar.get(Calendar.MONTH) + 1;
        int dayInt = calendar.get(Calendar.DAY_OF_MONTH);
        int hourInt = calendar.get(Calendar.HOUR_OF_DAY);
        int minuteInt = calendar.get(Calendar.MINUTE);

        if (isFieldSetAndPass(yearInt, year)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndPass(mothInt, moth)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndEquals(mothInt, moth) && isFieldSetAndPass(dayInt, day)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndEquals(mothInt, moth) && isFieldSetAndEquals(dayInt, day)
                && isFieldSetAndPass(hourInt, hour)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndEquals(mothInt, moth) && isFieldSetAndEquals(dayInt, day)
                && isFieldSetAndEquals(hourInt, hour) && isFieldSetAndPass(minuteInt, minute)) {
            return true;
        }
        return false;
    }

    /**
     * 日期可以选择当天而不报错
     */
    public boolean checkDateValidate() {
        int yearInt = calendar.get(Calendar.YEAR);
        int mothInt = calendar.get(Calendar.MONTH) + 1;
        int dayInt = calendar.get(Calendar.DAY_OF_MONTH);
        if (isFieldSetAndPass(yearInt, year)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndPass(mothInt, moth)) {
            return true;
        } else if (isFieldSetAndEquals(yearInt, year) && isFieldSetAndEquals(mothInt, moth)
                && isFieldSetAndNotPass(dayInt, day)) {
            return true;
        }
        return false;
    }

    /**
     * 时间校验，不能选择当前分钟
     */
    public boolean checkTimeValidate() {
        int hourInt = calendar.get(Calendar.HOUR_OF_DAY);
        int minuteInt = calendar.get(Calendar.MINUTE);
        if (isFieldSetAndPass(hourInt, hour)) {
            return true;
        } else if (isFieldSetAndEquals(hourInt, hour) && isFieldSetAndPass(minuteInt, minute)) {
            return true;
        }
        return false;
    }

    /**
     * 选择的参数（年月日时分）还未到达
     */
    private boolean isFieldSetAndPass(int intValue, String stringValue) {
        return (stringValue != null && !stringValue.isEmpty()) && Integer.parseInt(stringValue) > intValue;
    }

    /**
     * 选择的参数（年月日时分）相等
     */
    private boolean isFieldSetAndEquals(int intValue, String stringValue) {
        return (stringValue != null && !stringValue.isEmpty()) && Integer.parseInt(stringValue) == intValue;
    }

    /**
     * 选择的参数（年月日时分）还未到达，或者刚好相等
     */
    private boolean isFieldSetAndNotPass(int intValue, String stringValue) {
        return (stringValue != null && !stringValue.isEmpty()) && Integer.parseInt(stringValue) >= intValue;
    }
}
